package com.taimi.newdemo.service;

import java.util.Optional;

import com.taimi.newdemo.entity.UserEntity;
import com.taimi.newdemo.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public UserEntity findUserByUsername(String username) {
        Optional<UserEntity> optionalUser = userRepository.findByUsername(username);
        if (optionalUser.isPresent()) {
            return optionalUser.get();
        }
        return null;
    }

    public boolean authLogin(UserEntity userEntity) {
        Optional<UserEntity> optionalUser = userRepository.findByUsername(userEntity.getUsername());
        if (optionalUser.isPresent()) {
            UserEntity user = optionalUser.get();
            return user.getPassword() != null && user.getPassword().equals(userEntity.getPassword());
        }
        return false;
    }

    public UserEntity saveUser(UserEntity user) {
        return userRepository.save(user);
    }
}
